package logic

import (
	"context"
	"errors"
	"fmt"
	"gitignore/gitignorerpc/gitignorerpc"
	"gitignore/gitignorerpc/internal/svc"
	"gitignore/models/mysql"
	"math/rand"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *gitignorerpc.RegisterRequest) (*gitignorerpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line
	var findUser mysql.User

	if findUser.Phone_num == in.PhoneNum {
		return nil, errors.New("该手机号已注册")
	}

	user := mysql.User{
		//Model: gorm.Model{},
		//Model:       gorm.Model{},
		Phone_num:   in.PhoneNum,
		Password:    in.Password,
		Avatar_link: "http://t4kat4rvf.hn-bkt.clouddn.com/A445F06B110BC9DB2B56A03A8CEE4ECF.png",
		Nick_name:   fmt.Sprintf("用户_%s", rand.Intn(1000)),
	}

	err := user.FindUserByAdd(mysql.DB)
	if err != nil {
		return nil, errors.New("注册失败")
	}

	return &gitignorerpc.RegisterResponse{
		UserId: int64(user.ID),
	}, nil
}
